package controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.User;
import common.BaseServlet;
import common.CartShop;
import service.UserDAO;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/Login")
public class LoginServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
    UserDAO udao=new UserDAO();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
	public String basefun(HttpServletRequest request, HttpServletResponse response) {
    	request.setAttribute("requestURI", request.getParameter("requestURI"));
    	return "f:jsp/Login";
	}
    
    public void login(HttpServletRequest request, HttpServletResponse response) {
    	System.out.println("进入登录页面");
    	String telOrMail = request.getParameter("userTelOrMail");
    	String psw = request.getParameter("userPsw");
    	User user;
    	System.out.println(telOrMail+"--"+psw);
    	if(telOrMail.length()==11) {
    		user = udao.loginByTel(telOrMail,psw);
    	}else {
    		user = udao.loginByMail(telOrMail, psw);
    	}
    	
    	PrintWriter out;
    	try {
			out = response.getWriter();
			if(user!=null) {
				//登录成功后将user对象存入session中，修改ifLogin为1，
				//并将登录之前的购物车和数据库中用户的购物车进行合并
				HttpSession session=request.getSession(false);
				if(request.getSession(false)==null||session.getAttribute("cartShop")==null) {
					invalidateSession(session);
				}
		    	session.setAttribute("user", user);
		    	session.setAttribute("ifLogin", 1);
		    	System.out.println(user.getUserid());
		    	//将购物车合并
				CartShop cartShop=(CartShop) session.getAttribute("cartShop");
		    	System.out.println("未登录Userid:"+cartShop.getUserid());
				cartShop.LoginCartShop(user.getUserid());
		    	System.out.println("已登录Userid:"+cartShop.getUserid());
				session.setAttribute("cartShop", cartShop);
				//根据判断购物车为空存入ifEmpty
				session.setAttribute("ifEmpty",cartShop.ifEmpty());
				session.setMaxInactiveInterval(3600);
				
				out.print("1");
			}else {
				out.print("0");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    }
    
    //退出登录，将session中的user对象和cartshop对象清除
    public String logout(HttpServletRequest request, HttpServletResponse response) {
		HttpSession session=request.getSession();
		CartShop cartShop=(CartShop) session.getAttribute("cartShop");
		CartShop.getCarts().clear();
		cartShop.setUserid(0);
		session.setAttribute("ifEmpty", 1);
		session.setAttribute("ifLogin", 0);
		session.setAttribute("user", null);
		session.setAttribute("cartShop",cartShop);
    	return "f:jsp/Main";
	}

}
